Shader "UI/TextShadow"
{
    Properties
    {
        [PerRendererData] _MainTex ("Sprite Texture", 2D) = "white" {}
        _ShadowColor ("Shadow Color", Color) = (0,0,0,0.5)
        _ShadowOffset ("Shadow Offset", Vector) = (1, -1, 0, 0)
        _ShadowBlur ("Shadow Blur", Range(0, 2)) = 0.5
        _ShadowIntensity ("Shadow Intensity", Range(0, 1)) = 1
    }

    SubShader
    {
        Tags
        {
            "Queue"="Transparent"
            "IgnoreProjector"="True"
            "RenderType"="Transparent"
            "PreviewType"="Plane"
            "CanUseSpriteAtlas"="True"
        }

        Cull Off
        Lighting Off
        ZWrite Off
        ZTest [unity_GUIZTestMode]
        Blend SrcAlpha OneMinusSrcAlpha

        Pass
        {
            CGPROGRAM
            #pragma vertex vert
            #pragma fragment frag
            #pragma multi_compile __ SHADOW_BLUR_ENABLED
            
            #include "UnityCG.cginc"
            #include "UnityUI.cginc"

            struct appdata_t
            {
                float4 vertex   : POSITION;
                float4 color    : COLOR;
                float2 texcoord : TEXCOORD0;
            };

            struct v2f
            {
                float4 vertex   : SV_POSITION;
                fixed4 color    : COLOR;
                float2 texcoord : TEXCOORD0;
                float4 worldPosition : TEXCOORD1;
            };

            sampler2D _MainTex;
            fixed4 _Color;
            fixed4 _ShadowColor;
            float4 _ShadowOffset;
            float _ShadowBlur;
            float _ShadowIntensity;
            float4 _ClipRect;
            float4 _MainTex_ST;
            float4 _MainTex_TexelSize;

            v2f vert(appdata_t v)
            {
                v2f OUT;
                OUT.worldPosition = v.vertex;
                OUT.vertex = UnityObjectToClipPos(OUT.worldPosition);
                OUT.texcoord = TRANSFORM_TEX(v.texcoord, _MainTex);
                OUT.color = v.color * _Color;
                return OUT;
            }

            fixed4 frag(v2f IN) : SV_Target
            {
                // 计算阴影偏移
                float2 shadowOffset = _ShadowOffset.xy * _MainTex_TexelSize.xy;
                
                // 中心点采样
                half4 center = tex2D(_MainTex, IN.texcoord);
                
                // 阴影区域采样
                half4 shadow = half4(0, 0, 0, 0);
                
                #ifdef SHADOW_BLUR_ENABLED
                    // 模糊阴影采样
                    float blurRadius = _ShadowBlur;
                    float2 offsets[9] = {
                        float2(-blurRadius, blurRadius), float2(0, blurRadius), float2(blurRadius, blurRadius),
                        float2(-blurRadius, 0), float2(0, 0), float2(blurRadius, 0),
                        float2(-blurRadius, -blurRadius), float2(0, -blurRadius), float2(blurRadius, -blurRadius)
                    };
                    
                    // 高斯模糊权重
                    const float weights[9] = {
                        0.077847, 0.123317, 0.077847,
                        0.123317, 0.195346, 0.123317,
                        0.077847, 0.123317, 0.077847
                    };
                    
                    for(int i = 0; i < 9; i++)
                    {
                        float2 sampleUV = IN.texcoord + shadowOffset + offsets[i] * _MainTex_TexelSize.xy;
                        half4 sample = tex2D(_MainTex, sampleUV);
                        shadow += sample.a * weights[i];
                    }
                #else
                    // 无模糊阴影采样
                    shadow = tex2D(_MainTex, IN.texcoord + shadowOffset);
                #endif
                
                // 应用阴影强度
                shadow.a *= _ShadowIntensity;
                
                // 组合颜色
                fixed4 finalColor = _ShadowColor;
                finalColor.a *= shadow.a;
                
                // 添加文本
                finalColor.rgb = finalColor.rgb * (1.0 - center.a) + _Color.rgb * center.a;
                finalColor.a = max(finalColor.a, center.a);
                
                // UI裁剪
                finalColor.a *= UnityGet2DClipping(IN.worldPosition.xy, _ClipRect);
                
                return finalColor;
            }
            ENDCG
        }
    }
}